# Copyright (C) Nicola Bizzoca   2006/2007  <hannibal_smith@users.sourceforge.net>
# Copyright (C) Simone Economo   2006/2007  <ekoes@users.sourceforge.net>
# 
#    This file is part of Absynth-FTP.
#
#    Absynth-FTP is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    Absynth-FTP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk, TreeViewNew, TestSuite

class pageStore:
    
    def __init__(self, page, widget, number):
        self.page, self.widget, self.number = page, widget, number

class genericNoteBook:
    groups = {}
    count = {"groups": 0}
    
    def __init__(self, features):
        self.__features = features
        self.pages = {}
        self.count["pages"] = 0
        self.widget = gtk.Notebook()
        methods = {
            "show-border"  : "set_show_border",
            "show-tabs"    : "set_show_tabs",
            "show-arrows"  : "set_scrollable",
            "tabs-position": "set_tab_pos",
            "group-name"   : "set_group_id",
        }
        self.__setTabsPosition(self.__features["tabs-position"])
        if "group-name" in self.__features.keys():
            self.__setGroupName(self.__features["group-name"])
        for property, value in self.__features.items():
            try:
                getattr(self.widget, methods[property])(value)
            except:
                pass
        
        
    def __setTabsPosition(self, mode):
        position = {
            "left"  : gtk.POS_LEFT, 
            "right" : gtk.POS_RIGHT,
            "top"   : gtk.POS_TOP,
            "bottom": gtk.POS_BOTTOM,
        }
        self.__features["tabs-position"] = position[mode]
        
    def __setGroupName(self, name):
        if name in genericNoteBook.groups.keys():
            groupid = genericNoteBook.groups[name]
        else:
            groupid = genericNoteBook.count["groups"]
            genericNoteBook.groups[name] = groupid
        self.__features["group-name"] = groupid
        
    def addPage(self, image, text, widget, position = None, current = False):
        try:
            icon = gtk.image_new_from_stock(image, gtk.ICON_SIZE_MENU)
        except:
            icon = gtk.Image()
            icon.set_from_file(image)
        label = gtk.Label(text)
        hbox = gtk.HBox()
        hbox.add(icon)
        hbox.add(label)
        hbox.show_all()
        self.__appendNewPage(widget, hbox, position, current)
        
    def __appendNewPage(self, widget, page, position, current):
        if position is None:
            number = self.widget.append_page(widget, page)
        else:
            number = self.widget.insert_page(widget, page, position)
        if current is True:
            self.widget.set_current_page(number)
        methods = {
            "tabs-reorder" : "set_tab_reorderable",
            "tabs-detach"  : "set_tab_detachable",
        }
        if self.__features["tabs-reorder"] is True:
            getattr(self.widget, methods["tabs-reorder"])(widget, self.__features["tabs-reorder"])
        if self.__features["tabs-detach"] is True:
            getattr(self.widget, methods["tabs-detach"])(widget, self.__features["tabs-detach"])
        
        self.pages[self.count["pages"]] = pageStore(page, widget, number)
        self.count["pages"] += 1
    
    def getCurrentPage(self):
        return self.widget.get_current_page()
    
    def getWidgetFromPage(self, page):
        return self.widget.get_nth_page(page)
    
    def getPageFromWidget(self, widget):
        return self.widget.page_num(widget)
    
    def setPage(self, page):
        return self.widget.set_current_page(page)
    
    def removePage(self, page):
        return self.widget.remove_page(page)
        
def nestedSuite():
    localtree = TreeViewNew.genericListView({
        "headers-visible"  : True,
        "headers-clickable": True,
        "rows-search"      : True,
        "rows-same-height" : False,
        "rows-alternate"   : True,
        "rows-reorder"     : False,
    })
        
    localtree.addImageColumn("Filename", True, True)
    localtree.addTextColumn("-", str, False, False)
    localtree.addTextColumn("Size", str, True, False, 50)
    localtree.addTextColumn("Last Edit", str, True, True)
    localtree.mergeColumns(0, 1, None, None)
        
    localtree.addRow(["stock_folder", "usr", "4096", "aee"])
    localtree.addRow(["stock_folder", "bin", "4096", "?"])
    localtree.addRow(["stock_folder", "etc", "4096", "?"])
    localtree.addRow(["stock_folder", "var", "4096", "?"])
    localtree.addRow(["stock_folder", "local", "4096", "?"], position = 1)
        
    remotetree = TreeViewNew.genericListView({
        "headers-visible"  : True,
        "headers-clickable": True,
        "rows-search"      : True,
        "rows-same-height" : False,
        "rows-alternate"   : True,
        "rows-reorder"     : False,
    })
        
    remotetree.addImageColumn("Filename", True, True)
    remotetree.addTextColumn("-", str, False, False)
    remotetree.addTextColumn("Size", str, True, False, 50)
    remotetree.addTextColumn("Last Edit", str, True, True)
    remotetree.mergeColumns(0, 1, None, None)
        
    remotetree.addRow(["stock_folder", "usr2", "4096", "aee"])
    remotetree.addRow(["stock_folder", "bin2", "4096", "?"])
    remotetree.addRow(["stock_folder", "etc2", "4096", "?"])
    remotetree.addRow(["stock_folder", "var2", "4096", "?"])
    remotetree.addRow(["stock_folder", "local2", "4096", "?"], position = 1)

    notebook = genericNoteBook({
        "show-border"  : False,
        "show-tabs"    : False,
        "show-arrows"  : True,
        "tabs-reorder" : True,
        "tabs-detach"  : False,
        "tabs-position": "right",
    })
    notebook.addPage(gtk.STOCK_CDROM, "cdrommete", localtree.widget, None, False)
    notebook.addPage(gtk.STOCK_ABOUT, "aboute", remotetree.widget, None, True)
    print notebook.pages[1].number
    return notebook
            
if __name__ == "__main__":
    TestSuite.newSuite(nestedSuite).runTest()